export default {
  data() {
    return {
      dialogBatchImportTitle: '批量导入',
      currentSelectedDeptmentName: '',
      batchImportUrl: '/v1/datac/app/importData/excel',
      acceptFileType: '.xls,.xlsx,.XLS,.XLSX'
    }
  },
  methods: {
    // 导入部门
    handleBatchImport() {
      if (this.q_deptId === "") {
        this.$message({
          type: "info",
          message: "该节点为虚拟节点不可导入，请选择子部门"
        });
      } else {
        this.dialogType = "BATCHIMPORT";
        this.$nextTick(() => {
          this.$refs.fileUpload && this.$refs.fileUpload.clearFiles();
        })
      }
    },
    handleBatchImportSubmit() {
      this.$refs.fileUpload.submit()
    },
    handleBatchImportCancel() {
      this.$refs.fileUpload.clearFiles();
      this.dialogType = ''
    },
    handleFileSuccess(response, file, fileList) {
      console.log('success:::::')
    },
    handleFileRemove(file, fileList) {
      console.log('removeFile::::::')
    },
    handleFileChange(file, fileList) {
      console.log(file, fileList, 'fileChange:::::::')
    },
    handleBeforeUpload(file) {
      const fileArr = file.name.split('.')
      const fileType = fileArr[fileArr.length - 1]
      const fileTypeArr = ['xls', 'xlsx', 'XLS', 'XLSX']
      const isRightFileType = fileTypeArr.includes(fileType)
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!isRightFileType) {
        this.$message.error('上传文件只能是 xls或xlsx格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传excel文件大小不能超过 5MB!')
      }
      return isRightFileType && isLt5M
    },
    handleUpload(item) {
      const formData = new FormData()
      formData.append('file', item.file)
      formData.append('deptId', this.q_deptId)
      const params = {
        access_token: this.token,
        scope: this.websiteId
      }
      this.batchImport(formData, params).then(({
          code,
          message
        }) => {
          this.$message({
            type: code === "200" ? "success" : "error",
            message
          })
          if (code === "200") {
            this.$refs.table.load(1)
            this.dialogType = ''
          }
        })
        .catch(err => {
          console.error('/v1/datac/app/importData/excel:::::::', err)
        })
    }
  },
}
